<script setup lang="ts">
import { ref, watch } from 'vue'
import { useGetCompanyProjects } from '../composables/useGetCompanyProjects'
import { baseUrl } from '../config'
import { useRouter } from 'vue-router';

const router = useRouter()

const props = defineProps<{ companyId: number | null }>()

interface Project {
  id: number
  name: string
  comment: string
}

const projects = ref<Project[]>([])
const loading = ref(false)
const error = ref<string | null>(null)

async function fetchProjects(id: number) {
  loading.value = true
  error.value = null
  try {
    const { projects: p, fetchProjects } = useGetCompanyProjects(baseUrl, { id })
    await fetchProjects()
    projects.value = p.value
  } finally {
    loading.value = false
  }
}

watch(
  () => props.companyId,
  (id) => {
    if (id) fetchProjects(id)
    else projects.value = []
  },
  { immediate: true }
)

const emit = defineEmits(['edit', 'delete'])

function openModify(project: Project) {
  emit('edit', project.id)
}

function openProject(project: Project) {
  router.push({ name: 'Project', params: { projectId: project.id } })
}

function askDelete(project: Project) {
  emit('delete', project)
}

const showActionMenuId = ref<number | null>(null)

function toggleActionMenu(projectId: number) {
  showActionMenuId.value = showActionMenuId.value === projectId ? null : projectId
}
function closeActionMenu() {
  showActionMenuId.value = null
}
</script>

<template>
  <div class="project-list">
    <div class="project-title">项目列表</div>
    <ul>
      <li v-if="loading" class="project-item">加载中...</li>
      <li v-else-if="error" class="project-item">{{ error }}</li>
      <li v-else-if="projects.length === 0" class="project-item">暂无项目</li>
      <li
        v-else
        v-for="project in projects"
        :key="project.id"
        class="project-item"
        tabindex="0"
        @click="openProject(project)"
        @mouseleave="closeActionMenu"
      >
        <div class="project-header">
          <span class="project-name">{{ project.name }}</span>
          <div class="item-actions" @click.stop>
            <button class="action-menu-btn" @click="toggleActionMenu(project.id)">
              ⋮
            </button>
            <div
              v-if="showActionMenuId === project.id"
              class="action-menu"
            >
              <div class="action-menu-item" @click="openModify(project); closeActionMenu()">编辑</div>
              <div class="action-menu-item danger" @click="askDelete(project); closeActionMenu()">删除</div>
            </div>
          </div>
        </div>
        <div class="project-comment">{{ project.comment }}</div>
      </li>
    </ul>
  </div>
</template>

<style scoped>
.project-list {
  padding: 24px;
}
.project-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 12px;
}
ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
.project-item {
  padding: 14px 12px;
  border-radius: 8px;
  margin-bottom: 10px;
  background: #fafbfc;
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s;
  outline: none;
}
.project-item:hover,
.project-item:focus {
  background: #e6f7ff;
  box-shadow: 0 2px 8px rgba(24,144,255,0.08);
}
.project-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.project-name {
  font-size: 18px;
  font-weight: 600;
  color: #222;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.item-actions {
  position: relative;
  display: flex;
  align-items: center;
}
.action-menu-btn {
  background: transparent;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 2px 8px;
  border-radius: 4px;
  color: #888;
  transition: background 0.2s;
}
.action-menu-btn:hover {
  background: #e6f7ff;
  color: #1890ff;
}
.action-menu {
  position: absolute;
  top: 28px;
  right: 0;
  min-width: 80px;
  background: #fff;
  border: 1px solid #eee;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  z-index: 10;
  display: flex;
  flex-direction: column;
}
.action-menu-item {
  padding: 8px 16px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s, color 0.2s;
}
.action-menu-item:hover {
  background: #f5f5f5;
  color: #1890ff;
}
.action-menu-item.danger {
  color: #ff4d4f;
}
.action-menu-item.danger:hover {
  background: #fff0f0;
  color: #ff4d4f;
}
.project-comment {
  font-size: 13px;
  color: #888;
  line-height: 1.5;
  word-break: break-all;
}
</style>